﻿Imports MeoTom.Core

Partial Public Class Users
    Inherits UI.Page
    Private _isFirstLoad As Boolean = True

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    End Sub

    Public Function GenHtml(ByVal articlesID As String, ByVal Title As String, _
                            ByVal abstract As String, ByVal totalComment As String, _
                            ByVal viewCount As String, ByVal articlePath As String, _
                            ByVal isMedia As String, ByVal isPicture As String, _
                            ByVal releaseDate As String, ByVal userName As String, _
                            ByVal postVisible As String) As String
        Dim strMedia As String = ""
        Const strVideo As String = "&nbsp;<img src=""/images/hasVideo.gif"" style=""padding:0 0px 5px 0;float:none;"" width=""15"" height=""9"" alt=""Video"" />"
        Const strImage As String = "&nbsp;<img src=""/images/HasImage.gif"" style=""padding:0 0px 5px 0;float:none;"" width=""10"" height=""10"" alt=""Images"" />"

        If isMedia = "True" Then
            strMedia &= strVideo
        End If

        If isPicture = "True" Then
            strMedia &= strImage
        End If

        Dim strOut As New StringBuilder
        If postVisible = "2" Then
            strOut.Append(" <div class=""post-detail fl"">")
            strOut.Append("     <div class=""post-detail-left fl"">")
            strOut.Append("         <div class=""post-detail-bg""><span class=""total-cm"">").Append(TotalComment).Append("</span><br />Comments<br />").Append(viewCount).Append(" Views</div>")
            strOut.Append("     </div>")
            strOut.Append("     <div class=""post-detail-right fl"">")
            strOut.Append("         <h3><a style=""color:#FF0000;"" href=""").Append(articlePath).Append(""" title=""").Append(Title).Append(""">").Append(Title).Append("</a>").Append(strMedia).Append("</h3>")
            strOut.Append("         <div class=""post-desc"">").Append(Server.HtmlDecode(abstract).ToString).Append("...").Append("</div>")
            strOut.Append("         <div class=""post-info""><a href=""").Append(articlePath).Append(""" title=""").Append(Title).Append(""">").Append(Format(DateTime.Parse(releaseDate).AddHours(Configs.TimeLine), "dd-MM-yyyy HH:mm tt")).Append("</a> bởi <a href=""/Users/").Append(userName).Append(""" title=""").Append(userName).Append(""">").Append(userName).Append("</a> chuyên mục <span id=""ArticleCat___").Append(articlesID).Append("""></span></div>")
            strOut.Append("     </div>")
            strOut.Append(" </div>")
            strOut.Append(" <div class=""hr cut94 fl"">&nbsp;</div>")
        Else
            strOut.Append(" <div class=""post-detail fl"">")
            strOut.Append("     <div class=""post-detail-left fl"">")
            strOut.Append("         <div class=""post-detail-bg""><span class=""total-cm"">").Append(TotalComment).Append("</span><br />Comments<br />").Append(viewCount).Append(" Views</div>")
            strOut.Append("     </div>")
            strOut.Append("     <div class=""post-detail-right fl"">")
            strOut.Append("         <h3><a href=""").Append(articlePath).Append(""" title=""").Append(Title).Append(""">").Append(Title).Append("</a>").Append(strMedia).Append("</h3>")
            strOut.Append("         <div class=""post-desc"">").Append(Server.HtmlDecode(abstract).ToString).Append("...").Append("</div>")
            strOut.Append("         <div class=""post-info""><a href=""").Append(articlePath).Append(""" title=""").Append(Title).Append(""">").Append(Format(DateTime.Parse(releaseDate).AddHours(Configs.TimeLine), "dd-MM-yyyy HH:mm tt")).Append("</a> bởi <a href=""/Users/").Append(userName).Append(""" title=""").Append(userName).Append(""">").Append(userName).Append("</a> chuyên mục <span id=""ArticleCat___").Append(articlesID).Append("""></span></div>")
            strOut.Append("     </div>")
            strOut.Append(" </div>")
            strOut.Append(" <div class=""hr cut94 fl"">&nbsp;</div>")
        End If

        If _isFirstLoad Then
            TitleName.InnerHtml = "Xem tất cả bài viết bởi: " & userName

            Page.Title = "Xem tất cả bài viết bởi: " & userName & " - " & Configs.WebTitle
            Dim metaTag As New HtmlMeta()
            metaTag.Name = "description"
            metaTag.Content = "Xem tất cả bài viết bởi: " & userName & " - " & Configs.WebTitle
            Dim metaTag2 As New HtmlMeta()
            metaTag2.Name = "keywords"
            metaTag2.Content = userName & "," & Configs.WebTitle.Replace("-", ",")
            Header.Controls.Add(metaTag)
            Header.Controls.Add(metaTag2)

            _isFirstLoad = False
        End If

        Return strOut.ToString
    End Function

    Private Sub lqDataSource_Selecting(ByVal sender As Object, ByVal e As LinqDataSourceSelectEventArgs) Handles lqDataSource.Selecting
        If Request.QueryString("UserName") = vbNullString Then
            Response.Redirect("/Error?e=404")
        Else
            Dim strUserName As String = Request.QueryString("UserName")

            Using dc As New MeoTomDataContext
                e.Result = dc.Justin_Article_GetByUser(strUserName, Configs.SiteID).ToList
            End Using
        End If
    End Sub

    Private Sub lstArticle_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles lstArticle.PreRender
        If lstArticle.Items.Count = 0 Then
            Response.Redirect("/Error?e=NoArticle", False)
        End If
    End Sub

    Private Sub dtPgArticle_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles dtPgArticle.PreRender
        For Each control As UI.Control In dtPgArticle.Controls
            For Each c As UI.Control In control.Controls
                If TypeOf c Is HyperLink Then
                    Dim currentLink As HyperLink = DirectCast(c, HyperLink)
                    Dim match As Match = Regex.Match(currentLink.NavigateUrl, "Page=(?<Page123>.*?).*")
                    If match.Success Then
                        If Request.QueryString("Page") = vbNullString Then
                            currentLink.NavigateUrl = Request.RawUrl & "?" & match.Value
                        Else
                            currentLink.NavigateUrl = Request.RawUrl.Replace("Page=" & Request.QueryString("Page"), "") & match.Value
                        End If
                    End If
                End If
            Next
        Next
    End Sub
End Class